// #include "detail.hpp"


// // std::string uuid()
// // {
// //     std::stringstream ss;
// //     std::random_device rd;
// //     std::mt19937 genertor (rd());
// //     std::uniform_int_distribution<int> distribution(0, 255);
// //     for(int i = 0; i < 8; i++)
// //     {
// //         if(i == 4 || i == 6)
// //             ss << "-";
// //         ss << std::setw(2) << std::setfill('0') << std::hex << distribution(genertor);
// //     }

// //     static std::atomic<size_t> seq(1);
// //     size_t cur = seq.fetch_add(1);
// //     for(int i = 7; i >= 0; i--)
// //     {
// //         if(i == 5)
// //             ss << "-";
// //         ss << std::setw(2) << std::setfill('0') << std::hex << ((cur >> (i*8)) & 0xFF);
// //     }
// //     return ss.str();
// // }

// int main()
// {
//     int i = 8;
//     while(i--)
//     {
//         std::cout<<rpc::UUID::uuid()<<std::endl;
//     }
//     return 0;
// }


// #include "message.hpp"

// int main()
// {
    // rpc::RpcRequest::ptr rrp = rpc::MessageFactory::create<rpc::RpcRequest>();
    // Json::Value param;
    // param["num1"] = 11;
    // param["num2"] = 22;
    // rrp->setMethod("Add");
    // rrp->setParams(param);
    // std::string str = rrp->serialize();
    // std::cout<< str <<std::endl;

    // rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::MType::REQ_RPC);
    // if(!bmp->deserialize(str))
    //     return -1;
    // if(!bmp->check())
    //     return -1;
    // rpc::RpcRequest::ptr rrp2 = std::dynamic_pointer_cast<rpc::RpcRequest>(bmp);
    // std::cout<<rrp2->method() << std::endl;
    // std::cout<<rrp2->params()["num1"].asInt() << std::endl;
    // std::cout<<rrp2->params()["num2"].asInt() << std::endl;

    // rpc::TopicRequest::ptr trp = rpc::MessageFactory::create<rpc::TopicRequest>();
    // trp->setTopicKey("news");
    // trp->setOtype(rpc::TopicOptype::TOPIC_PUBLISH);
    // trp->setTopicMsg("hello world");
    // std::string str = trp->serialize();
    // std::cout<<str<<std::endl;

    // rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::MType::REQ_TOPIC);
    // if(!bmp->deserialize(str))
    //     return -1;
    // if(!bmp->check())
    //     return -1;


    // rpc::ServiceRequest::ptr srp = rpc::MessageFactory::create<rpc::ServiceRequest>();
    // srp->setMethod("Add");
    // srp->setOtype(rpc::ServiceOptype::SERVICE_REGISTRY);
    // srp->setHost({"127.0.0.1", 9090});
    // std::string str = srp->serialize();
    // std::cout<<str<<std::endl;

    // rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::MType::REQ_SERVICE);
    // if(!bmp->deserialize(str))
    //     return -1;
    // if(!bmp->check())
    //     return -1;

    // rpc::ServiceRequest::ptr srp2 = std::dynamic_pointer_cast<rpc::ServiceRequest>(bmp);
    // std::cout<<srp2->method()<<std::endl;
    // std::cout<< (int)srp2->optype()<<std::endl;
    // std::cout<< srp2->host().first <<std::endl;
    // std::cout<< srp2->host().second <<std::endl;

    // rpc::RPCResponse::ptr srp = rpc::MessageFactory::create<rpc::RPCResponse>();
    // srp->setRCode(rpc::RCode::RCODE_OK);
    // srp->setResult(33);
    // std::string str = srp->serialize();
    // std::cout<<str<<std::endl;

    // rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::MType::RSP_RPC);
    // if(!bmp->deserialize(str))
    //     return -1;
    // if(!bmp->check())
    //     return -1;

    // rpc::RPCResponse::ptr srp2 = std::dynamic_pointer_cast<rpc::RPCResponse>(bmp);
    // std::cout<<(int)srp2->rcode()<<std::endl;
    // std::cout<<srp2->result().asInt()<<std::endl;

    // rpc::TopicResponse::ptr srp = rpc::MessageFactory::create<rpc::TopicResponse>();
    // srp->setRCode(rpc::RCode::RCODE_OK);
    // std::string str = srp->serialize();
    // std::cout<<str<<std::endl;

    // rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::MType::RSP_TOPIC);
    // if(!bmp->deserialize(str))
    //     return -1;
    // if(!bmp->check())
    //     return -1;

    // rpc::TopicResponse::ptr srp2 = std::dynamic_pointer_cast<rpc::TopicResponse>(bmp);
    // std::cout<<(int)srp2->rcode()<<std::endl;


//     rpc::ServiceResponse::ptr srp = rpc::MessageFactory::create<rpc::ServiceResponse>();
//     srp->setRCode(rpc::RCode::RCODE_OK);
//     srp->setMethod("Add");
//     srp->setOtype(rpc::ServiceOptype::SERVICE_DISCOVERY);
//     std::vector<rpc::Address> addrs;
//     addrs.push_back({"127.0.0.1", 9090});
//     addrs.push_back({"127.0.0.2", 9091});
//     srp->setHost(addrs);
//     std::string str = srp->serialize();
//     std::cout<<str<<std::endl;

//     rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::MType::RSP_SERVICE);
//     if(!bmp->deserialize(str))
//         return -1;
//     if(!bmp->check())
//         return -1;

//     rpc::ServiceResponse::ptr srp2 = std::dynamic_pointer_cast<rpc::ServiceResponse>(bmp);
//     std::cout<<(int)srp2->rcode()<<std::endl;
//     std::cout<<(int)srp2->optype()<<std::endl;
//     for(auto& addr : srp2->hosts())
//         std::cout<<addr.first<<" " <<addr.second<<std::endl;
//     std::cout<<srp2->method()<<std::endl;
//     return 0;
// }


// #include "../common/dispatcher.hpp"
// #include "../common/message.hpp"
// #include "../common/net.hpp"
// #include "../server/rpc_router.hpp"
// #include <jsoncpp/json/value.h>
// #include <memory>

// void onRpcRequest(const rpc::BaseConnection::ptr& conn, rpc::RpcRequest::ptr& msg)
// {
//     std::cout<<"收到Rpc请求:";
//     std::string body = msg->serialize();
//     std::cout<<body<<std::endl;
//     auto rpc_rsp = rpc::MessageFactory::create<rpc::RPCResponse>();
//     rpc_rsp->setId("1234");
//     rpc_rsp->setMtype(rpc::MType::RSP_RPC);
//     rpc_rsp->setRCode(rpc::RCode::RCODE_OK);
//     rpc_rsp->setResult(33);
//     conn->send(rpc_rsp);
// }



// int main()
// {
//     auto dispatcher = std::make_shared<rpc::DisPatcher>();
//     //dispatcher->registerHandler<rpc::RpcRequest>(rpc::MType::REQ_RPC, onRpcRequest);
//     dispatcher->registerHandler<rpc::RpcRequest>(rpc::MType::REQ_RPC, onRpcRequest);

//     auto server = rpc::ServerFactory::create(9090);
//     auto message_cb = std::bind(&rpc::DisPatcher::onMessage, dispatcher.get(), std::placeholders::_1, std::placeholders::_2);
//     server->setMessageCallback(message_cb);
//     server->start();
//     return 0;
// }




// void Add(const Json::Value &req, Json::Value &rsp)
// {
//     int num1 = req["num1"].asInt();
//     int num2 = req["num2"].asInt();
//     rsp = num1 + num2;
// }
// int main()
// {
//     auto router = std::make_shared<rpc::server::RpcRouter>();
//     std::unique_ptr<rpc::server::SDescribeFactory> desc_factory(new rpc::server::SDescribeFactory());
//     desc_factory->setMethodName("Add");
//     desc_factory->setParamsDesc("num1", rpc::server::VType::INTEGRAL);
//     desc_factory->setParamsDesc("num2", rpc::server::VType::INTEGRAL);
//     desc_factory->setReturnType(rpc::server::VType::INTEGRAL);
//     desc_factory->setServiceCallBack(Add);
//     router->registerMethod(desc_factory->build());

//     auto cb = std::bind(&rpc::server::RpcRouter::onRpcRequest, router.get(), std::placeholders::_1,std::placeholders::_2);

//     auto dispatcher = std::make_shared<rpc::DisPatcher>();
//     dispatcher->registerHandler<rpc::RpcRequest>(rpc::MType::REQ_RPC, cb);
//     auto server = rpc::ServerFactory::create(9090);
//     auto message_cb = std::bind(&rpc::DisPatcher::onMessage, dispatcher.get(), std::placeholders::_1,std::placeholders::_2);
//     server->setMessageCallback(message_cb);
//     server->start();
    
//     return 0;
// }


#include "../common/detail.hpp"
#include "../server/rpc_server.hpp"

void Add(const Json::Value &req, Json::Value &rsp)
{
    int num1 = req["num1"].asInt();
    int num2 = req["num2"].asInt();
    rsp = num1 + num2;
}


int main()
{
    //auto router = std::make_shared<rpc::server::RpcRouter>();
    std::unique_ptr<rpc::server::SDescribeFactory> desc_factory(new rpc::server::SDescribeFactory());
    desc_factory->setMethodName("Add");
    desc_factory->setParamsDesc("num1", rpc::server::VType::INTEGRAL);
    desc_factory->setParamsDesc("num2", rpc::server::VType::INTEGRAL);
    desc_factory->setReturnType(rpc::server::VType::INTEGRAL);
    desc_factory->setServiceCallBack(Add);
    //router->registerMethod(desc_factory->build());

    rpc::server::RpcServer server(rpc::Address("127.0.0.1", 9090));
    server.registerMethod(desc_factory->build());
    server.start();

    return 0;
}